﻿using System.Net;
using RestSharp;

namespace Common.Util
{
    public class RestClientUtil
    {
        public static bool isCredentials = true;
        public static string UserName  = string.Empty;
        public static string Password = string.Empty;


        public static RestClient CreateRestClient(string baseUrl, int timeout = 5000)
        {
            var restclient = new RestClient(baseUrl);
            restclient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36";
            restclient.Timeout = timeout;

            return restclient;
        }

        public static RestClient CreateRestClient(string baseUrl, string daili, int timeout = 5000)
        {
            var restclient = new RestClient(baseUrl);
            restclient.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36";
            restclient.Timeout = timeout;
            if (daili != null)
            {
                restclient.Proxy = RestClientUtil.CreateWebProxy(daili);
            }

            return restclient;
        }

        public static WebProxy CreateWebProxy(string daili, bool BypassOnLocal=false)
        {
            var webProxy = new WebProxy(daili, BypassOnLocal);
            if (isCredentials) 
            {
                webProxy.Credentials = new NetworkCredential(userName: UserName, password: Password);
            }
       
            return webProxy;
        }

    }
}
